import 'package:flutter/material.dart';
import 'package:top_foundation/component/component.dart';
import 'package:top_foundation/utils/util.dart';

import '../../../utils/theme/theme.dart';
import 'index.dart';

class DropDownPage extends GetView<DropDownController> {
  const DropDownPage({Key? key}) : super(key: key);

  // 主视图
  Widget _buildView() {
    var state = controller.state;
    var md = controller.state.md;
    var frame = md.frame;
    return Column(
      // crossAxisAlignment: CrossAxisAlignment.end,
      crossAxisAlignment: CrossAxisAlignment.start,
      verticalDirection:
          frame.top == 0 ? VerticalDirection.up : VerticalDirection.down,
      children: [
        GestureDetector(
          onTap: controller.dismiss,
          child: Container(
            height: frame.top,
            color: Colors.transparent,
          ),
        ),
        if (md.child != null)
          Container(
            margin: EdgeInsets.only(bottom: frame.bottom, left: frame.left),
            width: frame.right,
            constraints: BoxConstraints(
              maxHeight: 0.7.sh,
              minHeight: 50.r,
            ),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(4.r),
            ),
            child: md.child,
          )
        else if (state.md.list != null)
          Container(
            margin: EdgeInsets.only(bottom: frame.bottom, left: frame.left),
            width: frame.right,
            constraints: BoxConstraints(
              maxHeight: 250.r,
              minHeight: 50.r,
            ),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(4.r),
            ),
            child: Card(
              margin: EdgeInsets.zero,
              child: ListView(
                padding: EdgeInsets.zero,
                shrinkWrap: true,
                controller: state.sc,
                children: state.md.list!
                    .asMap()
                    .map(
                      (key, e) => MapEntry(
                        key,
                        GestureDetector(
                          onTap: () {
                            state.current = key;
                            controller.onSure();
                          },
                          child: Container(
                            padding: EdgeInsets.symmetric(
                                horizontal: TopConfig.outerhHorizontalPadding,
                                vertical: 10.r),
                            // height: 50.r,
                            constraints: BoxConstraints(minHeight: 50.r),
                            decoration: UnderlineTabIndicator(
                                insets: EdgeInsets.symmetric(
                                    horizontal:
                                        TopConfig.outerhHorizontalPadding),
                                borderSide: const BorderSide(
                                    width: 1, color: Color(0xffeeeeee))),
                            child: SeparatorRow(
                              mainAxisAlignment: md.alignment,
                              children: [
                                if (key < (md.prefixList?.length ?? 0))
                                  md.prefixList![key],
                                Flexible(
                                  child: Text(
                                    e,
                                    style: TextStyle(
                                        color: state.current == key
                                            ? ITheme.selectedColor
                                            : const Color(0xff333333),
                                        fontSize: 13.sp),
                                  ),
                                )
                              ],
                              separatorBuilder: (index) => const SizedBox(
                                width: 10,
                              ),
                            ),
                          ),
                        ),
                      ),
                    )
                    .values
                    .toList(),
              ),
            ),
          ),
        Expanded(
          child: GestureDetector(
            onTap: controller.dismiss,
            child: Container(
              color: controller.state.md.frame.right >= 1.sw
                  ? Colors.black45
                  : Colors.transparent,
            ),
          ),
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<DropDownController>(
      builder: (_) {
        return Scaffold(
          backgroundColor: Colors.transparent,
          body: _buildView(),
        );
      },
    );
  }
}
